home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / Archive / XPK / xpk_Source / shell / xScan.c < prev    next >
C/C++ Source or Header  |  1998-11-15  |  5KB  |  219 lines

  1. #include <exec/alerts.h>
  2. #include <exec/memory.h>
  3. #include <exec/execbase.h>
  4.  
  5. #include <dos/dosextens.h>
  6. #include <dos/dosasl.h>
  7. #include <dos/rdargs.h>
  8.  
  9. #include <xpk/xpk.h>
  10.  
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13. #include <proto/xpkmaster.h>
  14.  
  15. #include <string.h>
  16.  
  17. #define XFH_ID "XFH A"
  18.  
  19. void PrintF(char *,...);
  20. void __regargs xScan(struct DosLibrary *,struct Library *,
  21.                      struct FileInfoBlock *,LONG);
  22.  
  23. struct xScanArgs
  24.  {
  25.   char **Files;
  26.   LONG Remove,All;
  27.  };
  28.  
  29. char *VersionString = "$VER: xScan 1.2 "__AMIGADATE__;
  30. char *Template = "FILE/M/A,REMOVE/S,ALL/S";
  31.  
  32. LONG __saveds main(void)
  33.  
  34. {
  35.  struct DosLibrary *DOSBase;
  36.  struct Library *XpkBase;
  37.  struct Process *MyProc;
  38.  struct RDArgs *RDArgs;
  39.  char ProgName[32];
  40.  LONG Result;
  41.  struct xScanArgs Args;
  42.  char **Files;
  43.  struct AnchorPath *AnchorPath;
  44.  
  45.  if ((MyProc=(struct Process *)FindTask(NULL))->pr_CLI==NULL)
  46.   {
  47.    (void)WaitPort(&MyProc->pr_MsgPort);
  48.    Forbid();
  49.    ReplyMsg (GetMsg(&MyProc->pr_MsgPort));
  50.  
  51.    return 0L;
  52.   }
  53.  
  54.  if ((DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",33L))==NULL)
  55.   {
  56.    Alert (AT_Recovery|AG_OpenLib|AO_DOSLib);
  57.  
  58.    return 20L;
  59.   }
  60.  if (DOSBase->dl_lib.lib_Version<37L)
  61.   {
  62.    (void)Write(Output(),"This program requires OS 2.04 or newer.\n",40L);
  63.  
  64.    CloseLibrary (&DOSBase->dl_lib);
  65.    return 20L;
  66.   }
  67.  
  68.  if (!GetProgramName(ProgName,32L)) (void)strcpy(ProgName,"xScan");
  69.  Result=10L;
  70.  
  71.  if ((XpkBase=OpenLibrary(XPKNAME,2L))==NULL)
  72.   {
  73.    PrintF ("%s: %s V2 or newer required !\n",ProgName,XPKNAME);
  74.  
  75.    goto Close1;
  76.   }
  77.  
  78.  Args.Remove=Args.All=FALSE;
  79.  if ((RDArgs=ReadArgs(Template,(LONG *)&Args,NULL))==NULL)
  80.   {
  81.    PrintFault (IoErr(),ProgName);
  82.  
  83.    goto Close2;
  84.   }
  85.  
  86.  if ((AnchorPath=(struct AnchorPath *)AllocVec(sizeof(struct AnchorPath),
  87.                                                MEMF_PUBLIC|MEMF_CLEAR))==NULL)
  88.   {
  89.    PrintFault (ERROR_NO_FREE_STORE,ProgName);
  90.  
  91.    goto Close3;
  92.   }
  93.  AnchorPath->ap_BreakBits=SIGBREAKF_CTRL_C;
  94.  AnchorPath->ap_Strlen=0;
  95.  
  96.  Files=Args.Files;
  97.  while (*Files)
  98.   {
  99.    LONG RetVal;
  100.  
  101.    for (RetVal=MatchFirst(*Files,AnchorPath); RetVal==0L; RetVal=MatchNext(AnchorPath))
  102.     {
  103.      if (AnchorPath->ap_Info.fib_DirEntryType<0L)
  104.       {
  105.        BPTR DirLock;
  106.  
  107.        DirLock=CurrentDir(AnchorPath->ap_Current->an_Lock);
  108.        xScan (DOSBase,XpkBase,&AnchorPath->ap_Info,Args.Remove);
  109.        (void)CurrentDir(DirLock);
  110.       }
  111.      else
  112.       if (AnchorPath->ap_Info.fib_DirEntryType!=ST_SOFTLINK)
  113.        {
  114.         if (((AnchorPath->ap_Flags&APF_DIDDIR)==0L)&&Args.All)
  115.          AnchorPath->ap_Flags|=APF_DODIR;
  116.         AnchorPath->ap_Flags&=~APF_DIDDIR;
  117.        }
  118.     }
  119.    MatchEnd (AnchorPath);
  120.  
  121.    if (RetVal!=ERROR_NO_MORE_ENTRIES)
  122.     {
  123.      PrintF ("%s: %s - ",ProgName,*Files);
  124.      PrintFault (RetVal,NULL);
  125.      
  126.      goto Close4;
  127.     }
  128.    else Files++;
  129.   }
  130.  Result=0L;
  131.  
  132. Close4: /* I hate "goto"s, but it's the only way to keep it short. */
  133.  FreeVec ((APTR)AnchorPath);
  134. Close3:
  135.  FreeArgs (RDArgs);
  136. Close2:
  137.  CloseLibrary (XpkBase);
  138. Close1:
  139.  CloseLibrary (&DOSBase->dl_lib);
  140.  return Result;
  141. }
  142.  
  143. void PrintF(char *FormatString,...)
  144.  
  145. {
  146.  struct DosLibrary *DOSBase;
  147.  
  148.  if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L))
  149.   {
  150.    (void)VPrintf(FormatString,(LONG *)&FormatString+1L);
  151.    (void)Flush(Output());
  152.  
  153.    CloseLibrary (&DOSBase->dl_lib);
  154.   }
  155. }
  156.  
  157. char __regargs *ULTA(char *Ptr,ULONG LW)
  158.  
  159. {
  160.  UWORD Index;
  161.  
  162.  *Ptr++=' ';
  163.  for (Index=0; Index<8; Index++, LW>>=4) Ptr[7-Index]='A'+(char)(LW&15);
  164.  return &Ptr[8];
  165. }
  166.  
  167. void __regargs xScan(struct DosLibrary *DOSBase,struct Library *XpkBase,
  168.                      struct FileInfoBlock *FIB,LONG Remove)
  169.  
  170. {
  171.  struct XpkFib XpkFib;
  172.  char ErrorBuffer[XPKERRMSGSIZE];
  173.  LONG Error;
  174.  
  175.  if(FIB->fib_Comment[0])
  176.   {
  177.    if(strncmp(FIB->fib_Comment,XFH_ID,strlen(XFH_ID))) return;
  178.   }
  179.  else
  180.   if (Remove) return;
  181.  
  182.  if (XpkExamineTags(&XpkFib,
  183.                     XPK_GetError,ErrorBuffer,
  184.                     XPK_InName,FIB->fib_FileName,TAG_DONE))
  185.   {
  186.    PrintF ("%s\n",ErrorBuffer);
  187.    return;
  188.   }
  189.  
  190.  if (Remove)
  191.   {
  192.    if (!SetComment(FIB->fib_FileName,""))
  193.     {
  194.      Error=IoErr();
  195.      PrintF ("Can't erase comment for %s: ");
  196.      PrintFault (Error,NULL);
  197.      return;
  198.     }
  199.   }
  200.  else
  201.   {
  202.    char Comment[80],*Ptr;
  203.  
  204.    Ptr=strcpy(Comment,XFH_ID)+strlen(XFH_ID);
  205.    Ptr=ULTA(Ptr,FIB->fib_Date.ds_Days);
  206.    Ptr=ULTA(Ptr,FIB->fib_Date.ds_Minute);
  207.    Ptr=ULTA(Ptr,FIB->fib_Date.ds_Tick);
  208.    Ptr=ULTA(Ptr,FIB->fib_Size);
  209.    *ULTA(Ptr,(XpkFib.xf_Type==XPKTYPE_PACKED)?XpkFib.xf_ULen:FIB->fib_Size)='\0';
  210.    if (!SetComment(FIB->fib_FileName,Comment))
  211.     {
  212.      Error=IoErr();
  213.      PrintF ("Can't set comment for %s: ");
  214.      PrintFault (Error,NULL);
  215.      return;
  216.     }
  217.   }
  218. }
  219.